/*
 * @Author: 王雁鹏
 * @Date: 2023-04-25 23:57:49
 * @LastEditTime: 2023-04-25 23:57:57
 * @LastEditors: 
 * @Description: 
 * @FilePath: \express\utils\encode.js
 * -------------------------------------
 */
const crypto = require('crypto')
const secret = '2019_06'
const algorithm = 'aes-256-cbc'
function encode(id) {
    const encoder = crypto.createCipher(algorithm, secret)
    const str = [id, Date.now(), '2019'].join('|')
    let encrypted = encoder.update(str, 'utf8', 'hex')
    encrypted += encoder.final('hex')
    return encrypted
}
function decode(str) {
    const decoder = crypto.createDecipher(algorithm, secret)
    let decoded = decoder.update(str, 'hex', 'utf8')
    decoded += decoder.final('utf8')
    const arr = decoded.split('|')
    return {
        id: arr[0],
        timespan: parseInt(arr[1])
    }
}
module.exports = {
    encode,
    decode
}